//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.cgreport.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import jakarta.servlet.http.HttpServletRequest;
import com.ht.entity.Result;
import com.ht.query.QueryGenerator;
import com.ht.modules.system.online.cgreport.entity.OnlCgreportHead;
import com.ht.modules.system.online.cgreport.entity.OnlCgreportItem;
import com.ht.modules.system.online.cgreport.entity.OnlCgreportParam;
import com.ht.modules.system.online.cgreport.model.OnlCgreportModel;
import com.ht.modules.system.online.cgreport.service.IOnlCgreportHeadService;
import com.ht.modules.system.online.cgreport.service.IOnlCgreportItemService;
import com.ht.modules.system.online.cgreport.service.IOnlCgreportParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/online/cgreport/head"})
public class OnlCgreportHeadController {
    private static final Logger log = LoggerFactory.getLogger(OnlCgreportHeadController.class);
    @Autowired
    private IOnlCgreportHeadService onlCgreportHeadService;
    @Autowired
    private IOnlCgreportParamService onlCgreportParamService;
    @Autowired
    private IOnlCgreportItemService onlCgreportItemService;

    public OnlCgreportHeadController() {
    }

    @GetMapping({"/parseSql"})
    public Result<?> parseSql(@RequestParam(name = "sql",required = true) String sql) {
        Map<String, Object> resultJson = new HashMap();
        List<OnlCgreportItem> onlCgreportItemList = new ArrayList<>();
        List<OnlCgreportParam> onlCgreportParamList = new ArrayList<>();
        List<String> fields = null;
        List<String> params = null;
        Object dbKey = null;

        try {
            fields = this.onlCgreportHeadService.getSqlFields(sql, (String)dbKey);
            params = this.onlCgreportHeadService.getSqlParams(sql);
            int i = 1;

            Iterator var13;
            String p;
            for(var13 = fields.iterator(); var13.hasNext(); ++i) {
                p = (String)var13.next();
                OnlCgreportItem t = new OnlCgreportItem();
                t.setFieldName(p);
                t.setFieldTxt(p);
                t.setIsShow(1);
                t.setOrderNum(i);
                t.setId(i + "_" + System.currentTimeMillis());
                t.setFieldType("String");
                onlCgreportItemList.add(t);
            }

            var13 = params.iterator();

            while(var13.hasNext()) {
                p = (String)var13.next();
                OnlCgreportParam param = new OnlCgreportParam();
                param.setParamName(p);
                param.setParamTxt(p);
                onlCgreportParamList.add(param);
            }

            resultJson.put("fields", onlCgreportItemList);
            resultJson.put("params", onlCgreportParamList);
        } catch (Exception var12) {
            log.error(var12.getMessage(), var12);
            String msg = "解析失败!<br><br>失败原因：";
            int i = var12.getMessage().indexOf("Connection refused: connect");
            if (i != -1) {
                msg = msg + "数据源连接失败.";
            } else {
                msg = msg + "SQL语法错误.";
            }

            Result.error(msg);
        }

        return Result.ok(resultJson);
    }

    @GetMapping({"/list"})
    public Result<IPage<OnlCgreportHead>> queryPageList(OnlCgreportHead onlCgreportHead, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<OnlCgreportHead>> result = new Result<>();
        QueryWrapper<OnlCgreportHead> queryWrapper = QueryGenerator.initQueryWrapper(onlCgreportHead, req.getParameterMap());
        Page<OnlCgreportHead> page = new Page((long)pageNo, (long)pageSize);
        IPage<OnlCgreportHead> pageList = this.onlCgreportHeadService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @PostMapping({"/add"})
    public Result<?> add(@RequestBody OnlCgreportModel values) {
        Result result = new Result<>();

        try {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            OnlCgreportHead head = values.getHead();
            List<OnlCgreportParam> params = values.getParams();
            List<OnlCgreportItem> items = values.getItems();
            head.setId(uuid);
            Iterator var7 = params.iterator();

            while(var7.hasNext()) {
                OnlCgreportParam param = (OnlCgreportParam)var7.next();
                param.setId((String)null);
                param.setCgrheadId(uuid);
            }

            var7 = items.iterator();

            while(var7.hasNext()) {
                OnlCgreportItem item = (OnlCgreportItem)var7.next();
                item.setId((String)null);
                item.setCgrheadId(uuid);
            }

            this.onlCgreportHeadService.save(head);
            this.onlCgreportParamService.saveBatch(params);
            this.onlCgreportItemService.saveBatch(items);
            result.success("添加成功！");
        } catch (Exception var9) {
            log.error(var9.getMessage(), var9);
            result.error500("操作失败");
        }

        return result;
    }

    @PutMapping({"/editAll"})
    public Result<?> editAll(@RequestBody OnlCgreportModel values) {
        try {
            return this.onlCgreportHeadService.editAll(values);
        } catch (Exception var3) {
            log.error(var3.getMessage(), var3);
            return Result.error("操作失败");
        }
    }

    @PutMapping({"/edit"})
    public Result<OnlCgreportHead> edit(@RequestBody OnlCgreportHead onlCgreportHead) {
        Result<OnlCgreportHead> result = new Result<>();
        OnlCgreportHead onlCgreportHeadEntity = (OnlCgreportHead)this.onlCgreportHeadService.getById(onlCgreportHead.getId());
        if (onlCgreportHeadEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = this.onlCgreportHeadService.updateById(onlCgreportHead);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    @DeleteMapping({"/delete"})
    public Result<OnlCgreportHead> delete(@RequestParam(name = "id",required = true) String id) {
        Result<OnlCgreportHead> result = new Result<>();
        OnlCgreportHead onlCgreportHead = (OnlCgreportHead)this.onlCgreportHeadService.getById(id);
        if (onlCgreportHead == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = this.onlCgreportHeadService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }

        return result;
    }

    @DeleteMapping({"/deleteBatch"})
    public Result<OnlCgreportHead> deleteBatch(@RequestParam(name = "ids",required = true) String ids) {
        Result<OnlCgreportHead> result = new Result<>();
        if (ids != null && !"".equals(ids.trim())) {
            this.onlCgreportHeadService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        } else {
            result.error500("参数不识别！");
        }

        return result;
    }

    @GetMapping({"/queryById"})
    public Result<OnlCgreportHead> queryById(@RequestParam(name = "id",required = true) String id) {
        Result<OnlCgreportHead> result = new Result<>();
        OnlCgreportHead onlCgreportHead = (OnlCgreportHead)this.onlCgreportHeadService.getById(id);
        if (onlCgreportHead == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlCgreportHead);
            result.setSuccess(true);
        }

        return result;
    }
}
